import storeLocal from 'storejs'
import { login, logout,getInfo } from '@/api/user'
import { resetRouter } from '@/router'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = {
	token: getToken(),
	routeMenu: [],
	menuList: [],
	userInfo: {}
}

const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_MENULIST: (state, menuList) => {
		storeLocal.set('menuList', menuList)
		state.menuList = menuList
	},
	SET_ROUTEMENU: (state, routeMenu) => {
		storeLocal.set('routeMenu', routeMenu)
		state.routeMenu = routeMenu
	},
	SET_USERINFO: (state, userInfo) => {
		storeLocal.set('userInfo', userInfo)
		state.userInfo = userInfo
	}
}

const actions = {
	// user login
	login({ commit }, userInfo) {
		const { username, password } = userInfo
		return new Promise((resolve, reject) => {
			login({ userName: username.trim(), password: password })
				.then(response => {
					// console.log('登录响应信息：'+response.token)
					console.log("登录请求返回数据：",response);
					const { data } = response
					if (!data) {
						reject('Login failed, please Login again.')
					}
					const { token, menuList } = data
					commit('SET_USERINFO', data)
					commit('SET_ROUTEMENU', menuList || [])
					commit('SET_TOKEN', data)
					setToken(data)
					resolve(response)
				})
				.catch(error => {
					console.log('登录异常：',error)
					reject(error)
				})
		})
	},
	// get user info
	getInfo({ commit, state }) {
		return new Promise((resolve, reject) => {
			/*try {
				let menuList = state.routeMenu.length > 0 ? state.routeMenu : storeLocal.get('routeMenu')
				commit('SET_MENULIST', menuList)
				resolve({ menuList })
			} catch (error) {
				reject('menuList not in userInfo!', error)
			}*/
			getInfo(state.token)
				.then(response => {
					const { data } = response
					console.log("响应info数据：",response)
					if (!data) {
						reject('获取信息失败，请重新登录.')
					}
					const {  name, avatar, introduction,menuList } = data

					// roles must be a non-empty array
					// if (!roles || roles.length <= 0) {
					// 	reject('getInfo: roles must be a non-null array!')
					// }
					commit('SET_MENULIST', menuList)
					// commit('SET_ROLES', roles)
					// commit('SET_NAME', name)
					// commit('SET_AVATAR', avatar)
					// commit('SET_INTRODUCTION', introduction)
					resolve(data)
				})
				.catch(error => {
					reject(error)
				})
		})
	},
	// user logout
	logout({ commit, state, dispatch }) {
		return new Promise((resolve, reject) => {
			logout(state.token)
				.then(() => {
					commit('SET_TOKEN', '')
					removeToken()
					resetRouter()
					dispatch('tagsView/delAllViews', null, { root: true })
					dispatch('permission/setDynamicAddedRoute', false, { root: true })
					localStorage.clear()
					resolve()
				})
				.catch(error => {
					reject(error)
				})
		})
	},

	// remove token
	resetToken({ commit }) {
		return new Promise(resolve => {
			commit('SET_TOKEN', '')
			removeToken()
			resolve()
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
